package com.system.goodstypeserver.controller;

import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.PagesBean;
import com.system.commonutils.pojo.GoodsType;
import com.system.goodstypeserver.entity.TempGoodsType;
import com.system.goodstypeserver.entity.TempGoodsType2;
import com.system.goodstypeserver.entity.TempGoodsType3;
import com.system.goodstypeserver.entity.TempGoodsType4;
import com.system.goodstypeserver.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ll
 * Date: 2020/07/07 9:23
 * Description:
 * Version: V1.0
 */
@RestController
@Api(value = "类型管理",tags = "类型管理-商品种类API")
@RequestMapping(value = "/goodsType")
public class GoodsTypeController {
    /*
     * 这里需要注意的是，springcloud默认返回xml文件，这点不同于springboot，
     * 如果希望修改返回json格式，则需要在RequestMapping中添加produces = MediaType.APPLICATION_JSON_UTF8_VALUE
     */

    @Autowired
    GoodsTypeService goodsTypeService;

//    @Autowired
//    TestService testService;

    @RequestMapping(value = "/queryGoods",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询商品种类接口",notes = "根据商品种类id或者商品类型名模糊查询",httpMethod = "POST", response = ApiResult.class)
    public ApiResult<PagesBean<GoodsType>> queryGoods(@RequestBody TempGoodsType goodsType){
        PagesBean pagesBean = new PagesBean();
        System.out.println("controller" + goodsType + "======" + "显示条数：" + goodsType.getPageSize() + "=====" + "当前页码：" + goodsType.getNowsPage());
        pagesBean.setNowsPage(goodsType.getNowsPage());//当前页码
        pagesBean.setPageSize(goodsType.getPageSize());//显示条数
        List<GoodsType> list= goodsTypeService.queryGoods(goodsType);
        System.out.println("创建时间：" + list.get(0).getGoodsType_createTime());
        if(list.size() > 0){
            int totalSize = goodsTypeService.queryTotalSize(goodsType);
//            System.out.println("总条数:" + totalSize);
            pagesBean.setDataCount(totalSize);//总条数
            pagesBean.setData(list);
            return ApiResult.successWithData("查询成功！", pagesBean);
        }else{
            return ApiResult.failure("查询失败！");
        }
    }

    @RequestMapping(value = "/addType",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "添加商品种类接口",notes = "添加新的商品类型以及备注",httpMethod = "POST", response = ApiResult.class)
    public ApiResult addType(@RequestBody TempGoodsType2 goodsType){
        System.out.println("controller:" + "goodsType_name:" + goodsType.getGoodsType_name() + "===" + "goodsType_notes:" + goodsType.getGoodsType_notes());
        //查询是否已存在
        if(goodsType.getGoodsType_name().equals("")){
            return ApiResult.failure("添加失败！未输入类型名！");
        }else{
            List<GoodsType> list = goodsTypeService.isExist(goodsType.getGoodsType_name());
            if(list.size() == 0){
                int result = goodsTypeService.addType(goodsType);
                if(result > 0){
                    return ApiResult.success("添加成功！");
                }else{
                    return ApiResult.failure("添加失败！");
                }
            }else{
                return ApiResult.failure("存在重复类型，添加失败！");
            }
        }
    }

    @RequestMapping(value = "/queryTypeById",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "编辑商品种类接口",notes = "根据商品类型id编辑商品类型以及备注",httpMethod = "POST", response = ApiResult.class)
    public ApiResult queryTypeById(@RequestBody TempGoodsType4 goodsType){
        //先显示出需要修改的类型数据
        List<TempGoodsType2> list = goodsTypeService.queryType(goodsType);
        if(list.size() > 0){
            return ApiResult.successWithData("查询成功！", list);
        }else{
            return ApiResult.failure("查询失败！请检查是否存在该类型！");
        }
    }

    @RequestMapping(value = "/editType",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "编辑商品种类接口",notes = "根据商品类型id编辑商品类型以及备注",httpMethod = "POST", response = ApiResult.class)
    public ApiResult editType(@RequestBody TempGoodsType3 goodsType){
        int result = goodsTypeService.editType(goodsType);
//        return commonReturn(result);
        if(result > 0){
            return ApiResult.success("编辑成功！");
        }else if(result == -1){
            return ApiResult.failure("编辑失败！存在重复商品类型！");
        }else{
            return ApiResult.failure("编辑失败！");
        }
    }

    @RequestMapping(value = "/delType",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除商品种类接口",notes = "根据商品类型id删除商品类型",httpMethod = "POST", response = ApiResult.class)
    public ApiResult delType(@RequestBody TempGoodsType4 goodsType){
        int result = goodsTypeService.delType(goodsType);
//        return commonReturn(result);
        if(result > 0){
            return ApiResult.success("删除成功！");
        }else{
            return ApiResult.failure("删除失败！此类型下有子产品！");
        }
    }

    @RequestMapping(value = "/queryType",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询种类接口",notes = "查询商品类型",httpMethod = "POST", response = ApiResult.class)
    public ApiResult queryType(){
        List<GoodsType> list = goodsTypeService.justQueryType();
        if(list.size() > 0){
            return ApiResult.successWithData("查询类型成功", list);
        }else{
            return ApiResult.failure("查询类型失败！");
        }
    }

//    private static ApiResult commonReturn(int result){
//        if(result > 0){
//            return ApiResult.success("编辑成功！");
//        }else{
//            return ApiResult.failure("编辑失败！存在重复商品类型！");
//        }
//    }
}
